﻿using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using eProject.Model;
using eProject.ViewModel;

namespace eProject.View
{
    /// <summary>
    /// Interaction logic for ActivityView.xaml
    /// </summary>
    public partial class ActivityView
    {
        public ActivityView()
        {
            InitializeComponent();
            var dpdcm = DependencyPropertyDescriptor.FromProperty(CommonManagerProperty, typeof(TimeGrid));
            dpdcm.AddValueChanged(this, OnValueChanged);     
            //this.
        }

        private void OnValueChanged(object sender, EventArgs e)
        {
            if (_commonManager != null)
            {
                Unlink(_commonManager);

            }

            _commonManager = CommonManager;

            CommonManager.PeriodChanged += OnPeriodChanged;
            INotifyCollectionChanged incc = _commonManager.Activities;
            incc.CollectionChanged += OnCollectionChanged;
            RefreshLeft();
        }

        private void Unlink(CommonManager manager)
        {
            manager.PeriodChanged -= OnPeriodChanged;
            INotifyCollectionChanged incc = manager.Activities;
            incc.CollectionChanged -= OnCollectionChanged;
        }

        private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.OldItems != null && e.OldItems.Contains(_viewModel))
            {
                Unlink(_commonManager);
                var dpdcm = DependencyPropertyDescriptor.FromProperty(CommonManagerProperty, typeof(TimeGrid));
                dpdcm.RemoveValueChanged(this, OnValueChanged);
                BindingOperations.ClearAllBindings(this);
            }
        }


        private ActivityViewModel _viewModel;

        private CommonManager _commonManager;

        public static readonly DependencyProperty CommonManagerProperty = DependencyProperty.Register("CommonManager",
                                                                                                      typeof (
                                                                                                          CommonManager),
                                                                                                      typeof (
                                                                                                          ActivityView
                                                                                                          ));

        public CommonManager CommonManager
        {
            get { return (CommonManager)GetValue(CommonManagerProperty); }
            set { SetValue(CommonManagerProperty, value); }
        }

        private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var oldactvm = e.OldValue as ActivityViewModel;
            if (oldactvm != null)
            {
                oldactvm.PropertyChanged -= OnPropertyChanged;
            }

            var actvm = e.NewValue as ActivityViewModel;
            if (actvm == null) throw new Exception("Illegal type of datacontext");//TODO new error message ;
            _viewModel = actvm;
            _viewModel.PropertyChanged += OnPropertyChanged;
            
            RefreshLeft();
            RefreshWidth();
        }

        private void OnPeriodChanged(Period oldOne, Period newOne)
        {
            if (newOne.Begin != oldOne.Begin) RefreshLeft();
        }

        private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName.Equals("Period"))
            {
                RefreshLeft();
                RefreshWidth();
            }
        }

        internal void RefreshWidth()
        {
            if (_viewModel == null) return;
            Width = _viewModel.Period.Duration*ViewConstants.PixelsPerDay;
        }

        internal void RefreshLeft()
        {
            if (_viewModel == null || CommonManager == null) return;
            var x = (_viewModel.Period.Begin - CommonManager.Period.Begin).TotalDays *
                    ViewConstants.PixelsPerDay - 2;
            Canvas.SetLeft(this, x);
        }
    }
}
